package demo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Restrictions;
import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.service.impl.CommonServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.hibernate.criterion.Restrictions;

import demo.entity.NotifyEntity;
import demo.service.NotifyServiceI;

@Service("notifyService")
@Transactional
public class NotifyServiceImpl extends CommonServiceImpl implements
		NotifyServiceI {

	@Override
	public List<NotifyEntity> query(Map<String, String> searchMap)
			throws Exception {
		// TODO Auto-generated method stub
		// 组装查询信息
		CriteriaQuery cq = new CriteriaQuery();
		if (searchMap.containsKey("searchType")) {
			// 查询类型
		}
		if (searchMap.containsKey("searchContent")) {
			// 查询内容
		}
		// 按照时间排序
		Map<String, Object> orderMap = new HashMap<String, Object>();
		orderMap.put("publishTime", "asc");
		cq.setOrder(orderMap);

		// 执行查询
		return this.list(cq);
	}

	@Override
	public PageMode<NotifyEntity> query4Page(int pageNo, int pageSize,
			Map<String, String> searchMap) throws Exception {
		// TODO Auto-generated method stub
		// 组装查询信息
		CriteriaQuery cq = new CriteriaQuery(NotifyEntity.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (searchMap.containsKey("searchType")) {
			// 查询类型
			cq.add(Restrictions.eq("categoryCode", searchMap.get("searchType")));
		}
		if (searchMap.containsKey("searchStart")) {
			// 查询起始时间
			cq.add(Restrictions.ge("publishTime",
					sdf.parse(searchMap.get("searchStart"))));
		}
		if (searchMap.containsKey("searchEnd")) {
			// 查询终止时间
			cq.add(Restrictions.le("publishTime",
					sdf.parse(searchMap.get("searchEnd"))));
		}
		// 按照时间排序
		Map<String, Object> orderMap = new HashMap<String, Object>();
		orderMap.put("publishTime", "desc");
		cq.setOrder(orderMap);

		// 执行查询
		// 分页问题，已修改，by wupan
//		return this.list4page(cq, pageSize, pageNo);
		return this.list4page(cq, pageNo, pageSize);
	}

}